package cn.iocoder.yudao.module.project.service.weightthresholdmanage;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.project.controller.admin.weightthresholdmanage.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.weightthresholdmanage.WeightThresholdManageDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.project.dal.mysql.weightthresholdmanage.WeightThresholdManageMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.project.enums.ErrorCodeConstants.*;

/**
 * 重量阈值管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WeightThresholdManageServiceImpl implements WeightThresholdManageService {

    @Resource
    private WeightThresholdManageMapper weightThresholdManageMapper;

    @Override
    public String createWeightThresholdManage(WeightThresholdManageSaveReqVO createReqVO) {
        // 插入
        WeightThresholdManageDO weightThresholdManage = BeanUtils.toBean(createReqVO, WeightThresholdManageDO.class);
        weightThresholdManageMapper.insert(weightThresholdManage);
        // 返回
        return weightThresholdManage.getId();
    }

    @Override
    public void updateWeightThresholdManage(WeightThresholdManageSaveReqVO updateReqVO) {
        // 校验存在
        validateWeightThresholdManageExists(updateReqVO.getId());
        // 更新
        WeightThresholdManageDO updateObj = BeanUtils.toBean(updateReqVO, WeightThresholdManageDO.class);
        weightThresholdManageMapper.updateById(updateObj);
    }

    @Override
    public void deleteWeightThresholdManage(String id) {
        // 校验存在
        validateWeightThresholdManageExists(id);
        // 删除
        weightThresholdManageMapper.deleteById(id);
    }

    private void validateWeightThresholdManageExists(String id) {
        if (weightThresholdManageMapper.selectById(id) == null) {
            throw exception(WEIGHT_THRESHOLD_MANAGE_NOT_EXISTS);
        }
    }

    @Override
    public WeightThresholdManageDO getWeightThresholdManage(String id) {
        return weightThresholdManageMapper.selectById(id);
    }

    @Override
    public PageResult<WeightThresholdManageDO> getWeightThresholdManagePage(WeightThresholdManagePageReqVO pageReqVO) {
        return weightThresholdManageMapper.selectPage(pageReqVO);
    }

    @Override
    public String saveWeightThresholdManage(WeightThresholdManageSaveReqVO reqVO) {
        String type = reqVO.getType();
        Double lowerWeight = reqVO.getLowerWeight();
        Double upperWeight = reqVO.getUpperWeight();

        if (StringUtils.isBlank(type)) {
            Assert.notNull(null,"产品型号不能为空");
        }

        // 判断该阈值是否已存在
        QueryWrapper<WeightThresholdManageDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", type);
        queryWrapper.eq("type", type);
        List<WeightThresholdManageDO> weightThresholdManageDOS = weightThresholdManageMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(weightThresholdManageDOS)) {
            WeightThresholdManageDO weightThresholdManageDO = weightThresholdManageDOS.get(0);
            weightThresholdManageDO.setLowerWeight(lowerWeight);
            weightThresholdManageDO.setUpperWeight(upperWeight);
            weightThresholdManageMapper.updateById(weightThresholdManageDO);
        }
        if (CollectionUtil.isEmpty(weightThresholdManageDOS)) {
            WeightThresholdManageDO weightThresholdManageDO = new WeightThresholdManageDO();
            weightThresholdManageDO.setId(type);
            weightThresholdManageDO.setType(type);
            weightThresholdManageDO.setLowerWeight(lowerWeight);
            weightThresholdManageDO.setUpperWeight(upperWeight);
            weightThresholdManageMapper.insert(weightThresholdManageDO);
        }
        return type;
    }

}